package com.aier.admin.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.admin.entity.AdminUser;
import com.aier.user.entity.User;
import com.aier.user.entity.mapper.UserRowMapper;

public class AdminUserRowMapper implements RowMapper<AdminUser> {

    private int type;

    public static final int ADMINUSER_AND_USER = 1;

    public AdminUserRowMapper() {
    }

    public AdminUserRowMapper(int type) {
        this.type = type;
    }

    @Override
    public AdminUser mapRow(ResultSet rs, int rowNum) throws SQLException {
        AdminUser au = new AdminUser();
        switch (type) {
            case ADMINUSER_AND_USER:
                au = parseUser(new AdminUser(), rs);
                au.setUser(UserRowMapper.parseUser(new User(), rs, "u"));
                break;
            default:
                au = parseUser(new AdminUser(), rs);
        }
        return au;
    }

    public static AdminUser parseUser(AdminUser u, ResultSet rs)
        throws SQLException {
        return parseUser(u, rs, null);
    }

    public static AdminUser parseUser(AdminUser u, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }

        try {
            u.setAid(rs.getInt(su + "aid"));
        } catch (SQLException e) {
            u.setAid(null);
        }

        return u;
    }

    public static Map<String, Object> getPar(Map<String, Object> par,
            AdminUser u) {
        par.put("aid", u.getAid());
        par.put("user", u.getUser());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            AdminUser u) {
        args.put("aid", u.getAid());
        return args;
    }

    /**
     * д�뷢�����ϵͳ������Ϣ��չ��
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final AdminUser u) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(u),
                        new String[] { "aid" });
                parsePS(ps, u);
                return ps;
            }

        };
    }

    private static String parseSQL(AdminUser u) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into ad(");
        int count = 0;
        if (null != u.getUser()) {
            sql.append("user");
            count++;
        }

        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, AdminUser u)
        throws SQLException {
        int count = 1;
        if (null != u.getUser()) {
            ps.setObject(count++, u.getUser());
        }

    }

}
